﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 邮件
    /// </summary>
    public class T_EmailDal
    {
        static SGArchiveSystem7xEntities conn;
        public static _ReturnValue Add_Email(T_Email en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_Email.Select(c => c).Where(c => c.E_EMAIL == en.E_EMAIL && c.E_TYPE == en.E_TYPE).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_Email.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        public static _ReturnValue Modify_Email(T_Email en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_Email.Select(c => c).Where(c => c.E_EMAIL == en.E_EMAIL && c.E_ID != en.E_ID && c.E_TYPE == en.E_TYPE).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_Email.Select(c => c).Where(c => c.E_ID == en.E_ID);
                    foreach (var n in query)
                    {
                        SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "E_ID");
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }


        public static _ReturnValue Del_Email(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_Email.Select(c => c).Where(c => c.E_ID == id);
                conn.T_Email.RemoveRange(query);
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        public static List<T_Email> Get_Email(int type,out int count, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_Email.Select(c => c);
                if (type > -1)
                {
                    query = query.Where(c => c.E_TYPE == type);
                }

                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderByDescending(c => c.E_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.ToList();
                    count = list.Count;
                    return list;
                }

               // count = query.Count();
               // return query.OrderByDescending(c => c.E_TYPE).OrderBy(c => c.E_ID).Skip(start).Take(row).ToList();
            }
            catch { return null; }
        }

        public static T_Email Get_Email(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_Email.Select(c => c).Where(c => c.E_ID == id).Single();
            }
            catch { return null; }
        }
    }
}
